package com.yesjpt.web;

import java.io.File;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.yesjpt.domain.FileInfo;
import com.yesjpt.serv.FileInfoServ;
import com.yesjpt.util.ContextUtil;
import com.yesjpt.util.L;
import io.swagger.annotations.Api;

@Api(tags = "文件")
@RestController
@RequestMapping("/file")
public class FileInfoController {

	//注入服务类
	@Autowired private FileInfoServ serv;
	
	
	@GetMapping("/hello") //http://localhost:8081/file/hello
	public Object hello() {
		L.begin(new Throwable().getStackTrace()[0]);
		return "hello";
	}
	//自动把java对象序列化为TCP/IP传输的8位组，采用JSON的技术
	@PostMapping("/upload")
	public FileInfo uploadFile(MultipartFile file) throws IOException {
		//调用ContextUtil.getParams()取得前端送过来的其它键值对
		//这样不用一个个在uploadFile()方法中申明
		L.i(new Throwable().getStackTrace()[0] + ", 其它提交的数据为：" + ContextUtil.getParams());
		return serv.save(file);
	}
	@PostMapping("/saveFile")
	public String saveFile(File file) throws IOException {
		//调用ContextUtil.getParams()取得前端送过来的其它键值对
		//这样不用一个个在uploadFile()方法中申明
		L.i(new Throwable().getStackTrace()[0] + ", 其它提交的数据为：" + ContextUtil.getParams() + ", name = " + file.getName());
		return "ok";
	}
}
